<template>
  <div>
    <common-head title="分类"></common-head>
    <div class="wrap">
      <div class="cate-container">
        <div
          v-for="(cate, index) in cates"
          :key="cate.id"
          @click="changeCate(index)"
          :class="[
            'cate',
            {
              active: index === num
            }
          ]"
        >
          {{ cate.name }}
        </div>
      </div>
      <div class="item-container">
        <div class="items">
          <div
          class="item"
          @click="clickItem(item.id)"
          v-for="item in items"
          :key="item.id"
        >
          <img class="item-pic" :src="item.pic" alt="" />
          <div class="item-info">
            <h5>{{ item.name | subStr }}</h5>
            <p>{{ item.characteristic | subStr }}</p>
            <div class="item-action">
              <p class="price">{{ item.minPrice | currency("￥") }}</p>
              <van-icon
                @click.stop="clickAddItem(item)"
                class-prefix="icon"
                name="jia-tianchong

"
              ></van-icon>
            </div>
          </div>
        </div>
        </div>
      </div>
    </div>
    <my-tabbar />
  </div>
</template>

<script>
import MyTabbar from '@components/MyTabbar'
import CommonHead from '@components/CommonHead'
import { fetchCate, fetchItems } from '@api'
import { currency, subStr } from '@/filters'
export default {
  name: 'cate',
  data () {
    return {
      cates: [],
      items: [],
      num: 0
    }
  },
  created () {
    this.fetchCates()
  },
  methods: {
    changeCate (index) {
      this.num = index
      this.fetchItems()
    },
    fetchCates () {
      fetchCate().then(res => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          this.fetchItems()
        }
      })
    },
    fetchItems () {
      fetchItems({
        categoryId: this.cates[this.num].id
      }).then(res => {
        if (res.data.code === 700) {
          this.items = []
        }
        if (res.data.code === 0) {
          this.items = res.data.data
        }
      })
    }
  },
  filters: {
    currency,
    subStr
  },
  components: {
    MyTabbar,
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.wrap {
  position: fixed;
  left: 0;
  right: 0;
  top: 46px;
  bottom: 50px;
  display: flex;
  .cate-container {
    width: 120px;
    height: 100%;
    border-right: 1px solid #333;
    background-color: rgb(218, 211, 211);
    .cate {
      line-height: 40px;
      text-align: center;
      color: #666;
      &.active {
        background-color: rgb(200, 197, 156);
        color: #fff;
      }
    }
  }
  .item-container {
    flex: 1;
    height: 100%;
    overflow: auto;
    .item {
      height: 150px;
      display: flex;
      background-color: #fff;
      margin-top: 10px;
      .item-pic {
        width: 150px;
        height: 150px;
      }
      .item-info {
        flex: 1;
        height: 150px;
        display: flex;
        flex-direction: column;
        padding: 0 10px;
        h5 {
          color: #333;
          font-size: 14px;
          line-height: 30px;
        }
        p {
          font-size: 12px;
          line-height: 28px;
          color: #666;
        }
        .item-action {
          height: 50px;
          display: flex;
          align-items: center;
          justify-content: space-between;
          .price {
            font-size: 16px;
            color: #f66;
          }
          .icon {
            font-size: 20px;
            color: #f66;
          }
        }
      }
    }
  }
}
</style>
